package com.ztz.mynewdemo.utils;


import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.internal.Util;

//网络访问工具类
public class HTTPUtils {


    public static final String CONTENT_TYPE_JSON = "application/json";

    //头值
    private static final String HEADER_X_LC_ID = "X-LC-Id";
    private static final String HEADER_X_LC_KEY = "X-LC-Key";
    private static final String HEADER_CONTENT_TYPE = "Content-Type";

    private static final String X_LC_ID_VALUE_1 = "S5KdTtqGDUwpImMvQ6HAJYaL-gzGzoHsz";
    private static final String X_LC_KEY_VALUE_1 = "NRPgCGiGxjbtGN2Bn0mlglrL";

    private static MediaType JSONTYPE = MediaType.parse("application/json;charset=utf-8");


    //Post请求
    public static Request requestPost(Object object, String url) {
        String jsonData = new Gson().toJson(object);
        RequestBody body = RequestBody.create(JSONTYPE, jsonData);
        Request request = new Request.Builder()
                .addHeader(HEADER_X_LC_ID,X_LC_ID_VALUE_1)
                .addHeader(HEADER_X_LC_KEY,X_LC_KEY_VALUE_1)
                .addHeader(HEADER_CONTENT_TYPE,CONTENT_TYPE_JSON)
                .url(url)
                .post(body)
                .build();
        return request;
    }


    //Get请求
    public static Request requestGet(String url) {
        Request request = new Request.Builder()
                .addHeader(HEADER_X_LC_ID,X_LC_ID_VALUE_1)
                .addHeader(HEADER_X_LC_KEY,X_LC_KEY_VALUE_1)
                .addHeader(HEADER_CONTENT_TYPE,CONTENT_TYPE_JSON)
                .url(url)
                .get()
                .build();
        return request;
    }

    //Delete请求
    public static Request requestDelete(String url){
        Request request = new Request.Builder()
                .addHeader(HEADER_X_LC_ID,X_LC_ID_VALUE_1)
                .addHeader(HEADER_X_LC_KEY,X_LC_KEY_VALUE_1)
                .addHeader(HEADER_CONTENT_TYPE,CONTENT_TYPE_JSON)
                .url(url)
                .delete()
                .build();
        return request;
    }

    //Post空值请求
    public static Request requestPostNull(String url){
        Request request = new Request.Builder()
                .addHeader(HEADER_X_LC_ID,X_LC_ID_VALUE_1)
                .addHeader(HEADER_X_LC_KEY,X_LC_KEY_VALUE_1)
                .addHeader(HEADER_CONTENT_TYPE,CONTENT_TYPE_JSON)
                .url(url)
                .post(Util.EMPTY_REQUEST)
                .build();
        return request;
    }

}
